﻿using Common;
using DAL;
using Model;
using ProgramLogicControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ETest
{
    public partial class SettingFrm : Form
    {
        List<Product> myProducts = new List<Product>();
        BindingSource myBindingSource = new BindingSource();
        public SettingFrm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cmbScanCom.Text == "" || cmbScanCom.Text == null)
            {
                MessageBox.Show("串口号不能为空");
                return;
            }
            else
            {
                ConfigHelper.WriteIni("SystemSettings", "ScanCom", cmbScanCom.Text);
            }
            if (cmbATEQCom.Text == "" || cmbATEQCom.Text == null)
            {
                MessageBox.Show("串口号不能为空");
                return;
            }
            else
            {
                ConfigHelper.WriteIni("SystemSettings", "ATEQCom", cmbATEQCom.Text);
            }

            if (tbScanAddr.Text == "")
            {
                MessageBox.Show("扫码完成寄存器地址不能为空");
                return;
            }
            else
            {
                ConfigHelper.WriteIni("SystemSettings", "PLCAddrScanCompeleted", tbScanAddr.Text);
            }
            if (tbATEQAddr.Text == "")
            {
                MessageBox.Show("PLC测试中寄存器地址不能为空");
                return;
            }
            else
            {
                ConfigHelper.WriteIni("SystemSettings", "PLCAddrTesting", tbATEQAddr.Text);
            }
            MessageBox.Show("系统配置保存成功！");
        }

        private void SettingFrm_Load(object sender, EventArgs e)
        {
            SqlSugarHelper sqlSugarHelper = new SqlSugarHelper();
            myProducts = sqlSugarHelper.QueryAll<Product>();
            myBindingSource.DataSource = myProducts;
            dataGridView1.DataSource = myBindingSource;
            //bindingNavigator1.BindingSource = myBindingSource;

            tbProductName.DataBindings.Add("TEXT", myBindingSource, "Name");
            tbCharacterCode.DataBindings.Add("TEXT", myBindingSource, "CharacterCode");
            tbCodeLength.DataBindings.Add("TEXT", myBindingSource, "Length");
            tbCClength.DataBindings.Add("TEXT", myBindingSource, "CCodeLength");
            tbCCodeLength.DataBindings.Add("TEXT", myBindingSource, "CCodeLocation");

            dataGridView1.Columns[0].Width = 50;
            dataGridView1.Columns[1].Width = 150;
            dataGridView1.Columns[2].Width = 300;
            dataGridView1.Columns[3].Width = 85;
            dataGridView1.Columns[4].Width = 85;
            dataGridView1.Columns[5].Width = 85;

            cmbScanCom.Text = ConfigHelper.ReadIni("SystemSettings", "ScanCom", "COM1", 255);
            cmbATEQCom.Text = ConfigHelper.ReadIni("SystemSettings", "ATEQCom", "COM2", 255);
            tbATEQAddr.Text = ConfigHelper.ReadIni("SystemSettings", "PLCAddrTesting", "M500", 255);
            tbScanAddr.Text = ConfigHelper.ReadIni("SystemSettings", "PLCAddrScanCompeleted", "M100", 255);
        }

        private void btnSaveProduct_Click(object sender, EventArgs e)
        {
            SqlSugarHelper sqlSugarHelper = new SqlSugarHelper();
            if (sqlSugarHelper.UpdateAll<Product>(myProducts))
            {
                MessageBox.Show("当前产品修改保存成功");
            }
        }
    }
}
